﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HZT.Framework.Web;
using HZT.Framework.Utility;
using HZT.Framework.Contract;

namespace AD.Web.Common
{
    public class ADController : AD.WebEx.ControllerBase
    {

        /// <summary>
        /// 登录后用户信息
        /// </summary>
        public virtual LoginInfo LoginInfo
        {
            get
            {
                return new LoginInfo();
            }
        }


        #region Override controller methods
        /// <summary>
        /// 方法执行前，如果没有登录就调整到Passport登录页面，没有权限就抛出信息
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
             var noAuthorizeAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeIgnoreAttribute), false);
             if (noAuthorizeAttributes.Length > 0)
                return;

            base.OnActionExecuting(filterContext);
            
            if (this.LoginInfo == null)
            {
                filterContext.Result = RedirectToAction("Login", "Auth", new { Area = "Account" });
                return;
            }

            //bool hasPermission = true;
            //var permissionAttributes = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(PermissionAttribute), false).Cast<PermissionAttribute>();
            //permissionAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(PermissionAttribute), false).Cast<PermissionAttribute>().Union(permissionAttributes);
            //var attributes = permissionAttributes as IList<PermissionAttribute> ?? permissionAttributes.ToList();
            //if (permissionAttributes != null && attributes.Count() > 0)
            //{
            //    hasPermission = true;
            //    foreach (var attr in attributes)
            //    {
            //        foreach (var permission in attr.Permissions)
            //        {
            //            if (!this.LoginInfo.BusinessPermissionList.Contains(permission))
            //            {
            //                hasPermission = false;
            //                break;
            //            }
            //        }
            //    }

            //    if (!hasPermission)
            //    {
            //        if (Request.UrlReferrer != null)
            //            filterContext.Result = this.Stop("没有权限！", Request.UrlReferrer.AbsoluteUri);
            //        else
            //            filterContext.Result = Content("没有权限！");
            //    }
            //}
        }

        /// <summary>
        /// 方法后执行后注入一些视图数据
        /// </summary>
        /// <param name="filterContext">filter context</param>
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.ActionDescriptor.ActionName.Contains("Edit") ||
                filterContext.ActionDescriptor.ActionName.Contains("Add"))
                return;

            RenderViewData();
        }

        /// <summary>
        /// 如果是Ajax请求的话，清除浏览器缓存
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
                filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
                filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                filterContext.HttpContext.Response.Cache.SetNoStore();
            }

            base.OnResultExecuted(filterContext);
        }

        /// <summary>
        /// 注入资源，权限，城市等信息
        /// </summary>
        protected override void RenderViewData()
        {
            //var permissions = string.Join(",", this.PermissionList);
            //this.ViewData["permissions"] = permissions;
        }

        #endregion
    }
}